<?php

require_once dirname(__FILE__) . '/fblogin/FBLoginController.php';
require_once dirname(__FILE__) . '/event/EventController.php';
require_once dirname(__FILE__) . '/event/AdminEventController.php';
require_once dirname(__FILE__) . '/users/AdminUserController.php';
require_once dirname(__FILE__) . '/staff/AdminStaffController.php';
require_once dirname(__FILE__) . '/navigation/NavigationView.php';
require_once dirname(__FILE__) . '/google/src/Google_Client.php';
require_once dirname(__FILE__) . '/google/src/contrib/Google_CalendarService.php';
require_once dirname(__FILE__) . '/Settings.php';

class MasterController {
	private $database;
	private $loginModel;
	private $usersModel;
	private $navigationView;

	public function __construct() {
		$db = new Database();
		$config = new DBConfig(Settings::DB_HOST, Settings::DB_USER, Settings::DB_PASSWORD, Settings::DB_DATABASE);
		$db->Connect($config);
		$this->database = $db;

		$facebook = new Facebook(array(
		  'appId'  => Settings::FB_APP_ID,
		  'secret' => Settings::FB_APP_SECRET,
		));

		$um = new UsersModel($db);		
		$this->loginModel = new FBLoginModel($facebook, $um);
		$this->usersModel = $um;
		$this->navigationView = new NavigationView();
	}

	/**
	* Kör loginkontrollern och kontrollern för den valda aktiviteten
	*
	* @return String HTML
	*/
	public function doControl() {
		$html = '';
		
		$html .= $this->handleLoginController();

		if ($this->loginModel->getUserRole() & UsersModel::ADMIN_ROLE) {
			$html .= $this->navigationView->getAdminMenu();
		} else if ($this->loginModel->getUserRole() & UsersModel::USER_ROLE) {
			$html .= $this->navigationView->getUserMenu();
		} else {
			$html .= $this->navigationView->getVisitorMenu();
		}

		$html .= $this->handleActiveController();

		$this->database->close();

		return $html;
	}

	private function handleLoginController() {
		$html = '';
        $fblc = new FBLoginController($this->loginModel, $this->usersModel);

        return $fblc->doControl($this->navigationView);
	}

	/**
	* Hjälpfunktion för att välja och köra den aktiva kontrollern
	*
	* @return String HTML
	*/
	private function handleActiveController() {
        $html = '';
        $activeController = null;
        $userViews = $this->navigationView->getActiveControllerType();

        switch ($userViews) {
        case NavigationView::ADMIN_EDIT_EVENTS :
	        $activeController = new AdminEventController($this->database, $this->loginModel, $this->navigationView);
	        break;

	    case NavigationView::ADMIN_EDIT_USERS :
	        $activeController = new AdminUserController($this->database, $this->loginModel, $this->navigationView);
	        break;

	    case NavigationView::ADMIN_EDIT_STAFF :
	    	$activeController = new AdminStaffController($this->database, $this->loginModel, $this->navigationView);
	    	break;

        case NavigationView::USER_VIEW_EVENTS :
            $activeController = new EventController($this->database, $this->loginModel, $this->navigationView);
            break;

        default:
            $activeController = new EventController($this->database, $this->loginModel, $this->navigationView);
        }
 
        return $activeController->doControl();
    }
}